package com.my.spvideo.filter;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.my.spvideo.enums.CompanyType;
import com.my.spvideo.util.user.SessionKeys;
import com.my.spvideo.util.user.User;


/**
 * 登录验证
 * @author 承
 *
 */
public class LoginFilter implements Filter {

	@Override
	public void destroy() {
		
	}
	
	private final Set<String> legalRequest = new HashSet<String>();
	private final Set<String> staticResource = new HashSet<String>();
	private volatile String default_jsp = null;
	
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest httpRequest = (HttpServletRequest) request;
		HttpServletResponse httpResponse = (HttpServletResponse) response;
		String path = httpRequest.getServletPath();
		System.out.println("path:"+path);
		//判断静态资源
		if(isStaticResource(path)){
			chain.doFilter(request,response);
			return ;
		}
		//判断合法请求
		if(legalRequest.contains(path)){
			chain.doFilter(request,response);
			return ;
		}
		HttpSession session = httpRequest.getSession(false);
		if(session!=null){
			User user = (User)session.getAttribute(SessionKeys.USER);
			if(user!= null){
				if(user.getProperty().getRoleId()==CompanyType.CHANNEL.ordinal()){
					if(!path.startsWith("/tables")&&!path.startsWith("/Statistics"))
						return;
				}
				chain.doFilter(request,response);
				return;
			}
		}
		//session过期或者未登录
		toDefaultJsp(httpRequest,httpResponse);
	}
	
	private void toDefaultJsp(HttpServletRequest httpRequest,HttpServletResponse httpResponse) throws IOException{
		if (httpRequest.getHeader("x-requested-with") != null
				&&httpRequest.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")){  
			//在响应头设置session状态  
			httpResponse.setHeader("sessionstatus", "timeout");	
			httpResponse.sendError(518);
			return;
        }  
		httpResponse.sendRedirect(getDefaultJsp(httpRequest));
	}
	
	private String getDefaultJsp(HttpServletRequest httpRequest){
		if(default_jsp==null){
			String tempPath = httpRequest.getScheme()+"://"+httpRequest.getServerName()+":"
					+httpRequest.getServerPort()+httpRequest.getContextPath()+"/login.jsp";
			default_jsp = tempPath;
		}
		return default_jsp;
	}
	
	/**
	 * 判断静态资源
	 * @param path
	 * @return
	 */
	private boolean isStaticResource(String path){
		int index = path.lastIndexOf(".");
		if(index==-1)
			return false;
		return staticResource.contains(path.substring(index));
	}  
	
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		staticResource.add(".swf");
		staticResource.add(".jpg");
		staticResource.add(".txt");
		staticResource.add(".gif");
		staticResource.add(".png");
		staticResource.add(".js");
		staticResource.add(".css");
		staticResource.add(".htm");
		staticResource.add(".WD3");
		staticResource.add(".eot");
		staticResource.add(".eot@");
		staticResource.add(".ttf");
		staticResource.add(".woff");
		legalRequest.add("/login.jsp");
		legalRequest.add("/login");
		legalRequest.add("/logout");
		legalRequest.add("/dd");
		legalRequest.add("/drawImage");
	}

}
